import path from "path";
import fs from "fs";

export default function pathCaseChecker() {
  return {
    name: "vite-plugin-path-case-checker",
    transform(code, id) {
      if (!id.includes("node_modules")) {
        const importRegex = /(?:import|from|require\()\s*['"]([^'"]+)['"]/g;
        let match;
        while ((match = importRegex.exec(code)) !== null) {
          const importPath = match[1];
          if (importPath.startsWith(".")) {
            const absPath = path.resolve(path.dirname(id), importPath);
            if (
              fs.existsSync(absPath) &&
              fs.realpathSync(absPath) !== absPath
            ) {
              console.warn(
                `⚠️ 路径大小写不匹配: 在文件 ${id} 中引用 ${importPath}`
              );
              console.warn(`实际路径应为: ${fs.realpathSync(absPath)}`);
            }
          }
        }
      }
      return null;
    },
  };
}
